var Ticket = {

    selected_row_id: null,
    objGrid: null,

    init: function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url: $('#table-url').val(),
            reload_after_add: true,
            dataType: 'json',
            colModel: [
                {display: 'Ticket No.', name: 'id', width: 60, sortable: true, align: 'left', hide: false},
                {display: 'Subject', name: 'subject', width: 200, sortable: true, align: 'left', hide: false},
                {display: 'Assign To', name: 'user_id', width: 150, sortable: true, align: 'left', hide: false},
                {display: 'Priority', name: 'priority_id', width: 100, sortable: true, align: 'left', hide: false},
                {display: 'Status', name: 'status_id', width: 80, sortable: true, align: 'left', hide: false},
                {display: 'Time Elapse', name: 'time_elapse', width: 80, sortable: false, align: 'left', hide: false}
            ],
            formFields: [
                {type: 'buttom', name: 'Update Ticket', bclass: 'edit', onpress: self.handleGridEvent},
                {separator: true},
                {type: 'buttom', name: 'Track Ticket', bclass: 'track', onpress: self.handleGridEvent}
            ],
            searchitems: [
                {display: 'Ticket No.', name: 'id', isdefault: true}
            ],
            checkboxSelection: true,
            editablegrid: {
                use: false
            },
            columnControl: false,
            sortname: "id",
            sortorder: "desc",
            usepager: true,
            useRp: true,
            rp: 15,
            showTableToggleBtn: false,
            height: 300,
            subgrid: {
                use: false
            }
        });
    },

    handleGridEvent: function (com, grid, json) {
        if (com == 'Update Ticket') {
            if (FlexObject.rowSelectedCheck(Ticket.objGrid, grid, 1)) {
                var row = FlexObject.getSelectedRowIds(grid);
                var url = $('#update_ticket_url').val() + '/' + row[0]
                //Ticket.objGrid.flexBeginEdit(row[0]);
                window.open(url, "UpdateTicket", "menubar=yes, width=800, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
            }
        }
        else if (com == 'Track Ticket') {
            if (FlexObject.rowSelectedCheck(Ticket.objGrid, grid, 1)) {
                var row = FlexObject.getSelectedRowIds(grid);
                var url = $('#track_ticket_url').val() + '/' + row[0]
                //Ticket.objGrid.flexBeginEdit(row[0]);
                window.open(url, "TrackTicket", "menubar=yes, width=800, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
            }
        }
    }
};

/* when the page is loaded */
$(document).ready(function () {
    Ticket.init();
});